package tech.aistar.day03.cookies;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet(urlPatterns = "/cookie/create")
public class CookieCreateController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建一个Cookie对象
        //如果value遇到中文如何???
        //HTTP Status 500 - Control character in cookie value or attribute.
        String name = "李晓楠lll";
        //一级编码
        name = URLEncoder.encode(name,"utf-8");
        //二级编码
        name = URLEncoder.encode(name,"utf-8");

        Cookie c = new Cookie("username",name);

        //设置域
        c.setPath("/");
        //设置过期时间 - s
        resp.addCookie(c);
    }
}
